package com.study.samsung.Control.htControl;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.samsung.Entity.Orders;
import com.study.samsung.Service.MyOrdersService;
import com.study.samsung.Utils.BaseServlet;
import com.study.samsung.Utils.OrderPager;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/htorders")
public class OrdersServlet extends BaseServlet {
    MyOrdersService ms = new MyOrdersService();
    @Override
    public String excute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Object obj = session.getAttribute("admin");
        if (obj==null){
            return "f:htjsp/login";
        }else {
            request.setCharacterEncoding("utf-8");
            response.setContentType("application/json;charset=utf-8");
            Integer pageNo=1,pageSize=10;
            String[] arr = new String[5];
            OrderPager pager=ms.selectByPager(1,10,arr);
            session.setAttribute("orderpager",pager);
            return "f:htjsp/orders";
        }

    }
    public JSONObject orderfenye(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Integer pageno = Integer.parseInt(request.getParameter("pageno"));
        String arr = request.getParameter("arr");
        JSONArray objects = JSONArray.parseArray(arr);
        String[] arrs = new String[objects.size()];

        for (int i = 0; i < arrs.length; i++) {
            arrs[i]= objects.get(i).toString();
        }
        OrderPager pager = ms.selectByPager(pageno, 10, arrs);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pager",pager);
        return jsonObject;
    }

    public String orderDetail(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        String id = request.getParameter("did");
        Integer did = Integer.parseInt(id);
        session.setAttribute("htdid",did);
        List<Orders> list = ms.selectOrderShade(did);
        String json = JSONObject.toJSONString(list);
        System.out.println(json);
        return json;
    }

    public void updateStatus(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        Integer did =(Integer) session.getAttribute("htdid");
        Integer num = ms.updateStatus(did);
        if (num==-1){
            out.println("0");
        }else{
            out.println("1");
        }
        out.close();
    }
}
